package com.coffee.order.service.inquire.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.inquire.query.InquireBuyerConfirmPlanQuery;
import com.coffee.order.api.inquire.result.InquireBuyerConfirmPlanResult;
import com.coffee.order.api.inquire.service.InquireBuyerConfirmPlanService;
import com.coffee.order.service.inquire.bean.InquireBuyerConfirmPlan;
import com.coffee.order.service.inquire.dao.InquireBuyerConfirmPlanDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class InquireBuyerConfirmPlanServiceImpl implements InquireBuyerConfirmPlanService {
	@Resource
	private InquireBuyerConfirmPlanDao	inquireBuyerConfirmPlanDao;

	@Override
	public Page<InquireBuyerConfirmPlanResult> getPager(InquireBuyerConfirmPlanQuery param, PagerInfo pr) {
		Page<InquireBuyerConfirmPlanResult> pager=new Page<InquireBuyerConfirmPlanResult>(pr.getStart(), pr.getPageSize());
		Integer count=inquireBuyerConfirmPlanDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<InquireBuyerConfirmPlan> list=inquireBuyerConfirmPlanDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, InquireBuyerConfirmPlanResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(InquireBuyerConfirmPlanQuery param){
		Integer count=inquireBuyerConfirmPlanDao.count(param);
		return count;
	}
	
	@Override
	public InquireBuyerConfirmPlanResult getDetail(Integer id){
		InquireBuyerConfirmPlan item=inquireBuyerConfirmPlanDao.findById(id);
		if(item==null){
			return null;
		}
		InquireBuyerConfirmPlanResult result=new InquireBuyerConfirmPlanResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<InquireBuyerConfirmPlanResult> getList(InquireBuyerConfirmPlanQuery param, PagerInfo pr){
		List<InquireBuyerConfirmPlan> list=inquireBuyerConfirmPlanDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, InquireBuyerConfirmPlanResult.class);
	}
	
	@Override
	public List<InquireBuyerConfirmPlanResult> getList(InquireBuyerConfirmPlanQuery param){
		List<InquireBuyerConfirmPlan> list=inquireBuyerConfirmPlanDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, InquireBuyerConfirmPlanResult.class);
	}
	
	@Override
	public List<InquireBuyerConfirmPlanResult> getList(){
		InquireBuyerConfirmPlanQuery param = new InquireBuyerConfirmPlanQuery();
		List<InquireBuyerConfirmPlan> list=inquireBuyerConfirmPlanDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, InquireBuyerConfirmPlanResult.class);
	}


	@Override
	public void delete(Integer id) {
		inquireBuyerConfirmPlanDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(InquireBuyerConfirmPlanResult result)throws BusinessException {
		InquireBuyerConfirmPlan item=new InquireBuyerConfirmPlan();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			inquireBuyerConfirmPlanDao.insert(item);
		}else {
			InquireBuyerConfirmPlan tmp=inquireBuyerConfirmPlanDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			inquireBuyerConfirmPlanDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,InquireBuyerConfirmPlanResult> getItemMap(Set<Integer> idSet){
		Map<Integer, InquireBuyerConfirmPlanResult> map=new HashMap<Integer, InquireBuyerConfirmPlanResult>();
		List<InquireBuyerConfirmPlan> list=inquireBuyerConfirmPlanDao.findByIds(StringUtil.setToList(idSet));
		List<InquireBuyerConfirmPlanResult> resultList=DTOUtils.convertList(list, InquireBuyerConfirmPlanResult.class);
		for(InquireBuyerConfirmPlanResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,InquireBuyerConfirmPlanResult> getItemMap(){
		Map<Integer, InquireBuyerConfirmPlanResult> map=new HashMap<Integer, InquireBuyerConfirmPlanResult>();
		InquireBuyerConfirmPlanQuery param=new InquireBuyerConfirmPlanQuery();
		List<InquireBuyerConfirmPlan> list=inquireBuyerConfirmPlanDao.page(param, 0, Integer.MAX_VALUE);
		List<InquireBuyerConfirmPlanResult> resultList=DTOUtils.convertList(list, InquireBuyerConfirmPlanResult.class);
		for(InquireBuyerConfirmPlanResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
